package net.xuele.learn.base;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by  on 2019/4/25 0025.
 */
public class MappedByteBufferCase {
    public static void main(String[] args) {
        File f = new File("D://data.txt");
        long len = f.length();
        byte[] bytes = new byte[(int) len];

        try {
            MappedByteBuffer mapped = new RandomAccessFile(f, "r")
                    .getChannel()
                    .map(FileChannel.MapMode.READ_ONLY, 0, len / 2 - 2);
            byte[] set = "我加上去的嗷~~~~".getBytes();
            mapped.position((int) len);
            mapped.limit((int) (set.length + len));
            mapped.get(set);
            mapped.force();
            //for (int offset = 0; offset < len / 2 - 2; offset++) {
            //    byte b = mapped.get(offset);
            //    bytes[offset] = b;
            //}
            //Scanner scanner = new Scanner(new ByteArrayInputStream(bytes)).useDelimiter(" ");
            //while (scanner.hasNext()) {
            //    System.out.println(scanner.next() + " ");
            //}
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
